//
//  ItemOfZheKou.m
//  SeeTheWorld
//
//  Created by dllo on 16/3/2.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ItemOfZheKou.h"
#import "ModeZheKou.h"
#import "PCH.pch"
#import "CellZheKou.h"
#import <UIImageView+WebCache.h>


@interface ItemOfZheKou () <UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain)UICollectionView *collectionView;
@property (nonatomic, retain)UICollectionViewFlowLayout *layout;
@property (nonatomic, retain)NSMutableArray *arrayOfModel;

@property (nonatomic, retain)NSString *text;


@end

@implementation ItemOfZheKou

- (void)dealloc {
    self.collectionView.delegate = nil;
    self.collectionView.dataSource = nil;
    [_arrayOfModel release];
    [_layout release];
    [_collectionView release];
    [_text release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        self.arrayOfModel = [NSMutableArray array];
        
        self.layout = [[UICollectionViewFlowLayout alloc] init];
        self.layout.minimumInteritemSpacing = 20;
        self.layout.minimumLineSpacing = 20;
        self.layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        self.layout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
        self.layout.itemSize = CGSizeMake((self.contentView.frame.size.width - 60) / 2.0, (self.contentView.frame.size.width - 60) / 2 * 1.15);
        
        self.collectionView = [[UICollectionView alloc] initWithFrame:self.contentView.frame collectionViewLayout:self.layout];
        [self.contentView addSubview:self.collectionView];
        [_collectionView release];
        self.collectionView.backgroundColor = [UIColor whiteColor];
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
    //    self.collectionView.dk_backgroundColorPicker = DKColorWithRGB(0xffffff, 0x343434);
        self.collectionView.backgroundColor = [UIColor colorWithRed:224 / 256.f green:224 / 256.f blue:224 / 256.f alpha:0.5];
        [self.collectionView registerClass:[CellZheKou class] forCellWithReuseIdentifier:@"pool"];
        
        
        
        
    }
    
    return self;
    
}

- (void)passText:(NSString *)text {
    
    self.text = text;
    
  //  NSString *str1 = @"%7";
    NSString *str2 =@"%2";
    
    NSString *str = [NSString stringWithFormat:@"http://open.qyer.com/lastminute/app_lastminute_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&kw=%@&page=1&track_app_channel=App%@520Store&track_app_version=6.8.4&track_device_info=iPhone%@5205s&track_deviceid=860F3F2D-126C-4AC3-A97D-A53DF488AB23&track_os=ios%@5209.2.1&v=1", self.text, str2, str2, str2];
    
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
    
    NSURL *URL = [NSURL URLWithString:str];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    
    NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
        if (error) {
            NSLog(@"Error: %@", error);
        } else {
            
            for (NSDictionary *dic in [[responseObject objectForKey:@"data"] objectForKey:@"lastminutes"]) {
                ModeZheKou *model = [[ModeZheKou alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [self.arrayOfModel addObject:model];
                [model release];
            }
            
        }
        
        [self.collectionView reloadData];
        
    }
                                      
  ];
    
    [dataTask resume];
    
    
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.arrayOfModel.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CellZheKou *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    [cell passModel:[self.arrayOfModel objectAtIndex:indexPath.item]];
    
    return cell;
    
}



@end
